package com.example.qinghe.frontController.communication;

import com.alibaba.fastjson.JSON;
import com.example.qinghe.entity.Common;
import com.example.qinghe.entity.communication.Enterprise;
import com.example.qinghe.service.communication.EnterpriseService;
import com.example.qinghe.service.communication.StaffService;
import com.example.qinghe.service.communication.UserService;
import com.example.qinghe.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * AdminController
 * 
 * @author ruoyi
 * @date 2023-07-29
 */
@RestController
@RequestMapping("/front/enterprise")
@Validated
@CrossOrigin(origins = "*", maxAge = 3600,allowCredentials="true")
//swagger

@Api(tags = "移动通讯录->组织架构", description = "FrontEnterpriseController")
public class FrontEnterpriseController
{
    @Autowired
    private EnterpriseService service;
    @Autowired
    private StaffService staffService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserService userService;



    @ApiOperation("组织：查找组织内的信息，如果type=1表示该组织下没有其他组织了,返回的list是人员信息列表")
    @GetMapping("/sel")
    public Result latestInfo(@ApiParam(name = "id",value = "根据组织id查找下面的信息") @RequestParam(required = true) Long id
    )
    {
        List<Enterprise> sel = service.sel(id);
        Map map=new HashMap<>();
        System.out.println("sel:"+(sel.size()));
        if(sel.size()==0)
        {
            map.put("type",1);
            Common common=new Common();
            common.put("enterprise_id", id);
            List<Common> list = staffService.list(common);
            map.put("list",list);
            System.out.println("list:"+list);
            return Result.success(map);
        }
        map.put("type",0);
        map.put("list",sel);
        return Result.success(map);
    }

    @ApiOperation("转移数据，管理员用的")
    @GetMapping("/move")
    public void move()
    {
       List<Enterprise> enterprises=service.selAll();
        for (int i = 0; i < enterprises.size(); i++) {
            System.out.println(JSON.toJSONString(enterprises.get(i)));
            if(enterprises.get(i)!=null&&enterprises.get(i).getParent_id()!=null)
            {
                enterprises.get(i).setId(enterprises.get(i).getId()+100);
                if(enterprises.get(i).getParent_id()!=1)
                enterprises.get(i).setParent_id(enterprises.get(i).getParent_id()+100);
                service.adddepart(enterprises.get(i));
            }

        }
    }




}
